package com.solocredit.finance.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.solocredit.finance.R
import com.solocredit.finance.databinding.ActMullaPaPoyBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.ProAct
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper

class MullaPaPoyAct : ProAct() {

    private lateinit var binding: ActMullaPaPoyBinding
    private var linkTitle = ""

    companion object {
        private const val INTENT_LINK_TITLE = "linkTitle"
        private const val INTENT_LINK_URL = "linkUrl"
        fun start(context: Context, title: String, linkUrl: String) {
            val intent = Intent(context, MullaPaPoyAct::class.java)
            intent.putExtra(INTENT_LINK_TITLE,title)
            intent.putExtra(INTENT_LINK_URL, linkUrl)
            context.startActivity(intent)
        }
    }

    override fun getMullaRootView(): View {
        binding = ActMullaPaPoyBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaTextTitle(getLinkTitle())
        setMullaTextTitleGravity(Gravity.CENTER)
        setMullaRightBtnRes(R.mipmap.mulla_kefu_icon)

        initWebViewSettings()
    }

    override fun initMullaListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showMullaKefuDialog("$linkTitle Page")
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_CUSTOMER,"$linkTitle Page")
        }
        rootViewBinding.ivLanguage.setOnClickListener {
            HttpHelper.addMullaEventAction(MullaEventType.CLICK.MULLA_CLICK_LANGUAGE,"$linkTitle Page")
            showMullaLanguageDialog("$linkTitle Page", onSelectCallback = {
            })
        }
    }

    override fun initMullaData() {
        //牌照信息
        val licenseInfo = MullaCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }

        binding.ppWebView.loadUrl(getLinkUrl())
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        binding.ppWebView.setInitialScale(100)
        binding.ppWebView.webChromeClient = mWebChromeClient
        binding.ppWebView.webViewClient = mWebClient
        val webSettings = binding.ppWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            binding.progressBar.progress = newProgress
            if (newProgress == 100) {
                binding.progressBar.visibility = View.GONE
            } else {
                binding.progressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?, ): Boolean {
            return shouldOverrideUrlLoading(view, request?.url.toString())
        }

        @Deprecated("Deprecated in Java", ReplaceWith("false"))
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    fun getLinkTitle(): String {
        return intent.extras?.getString(INTENT_LINK_TITLE).toString()
    }

    fun getLinkUrl(): String {
        return intent.extras?.getString(INTENT_LINK_URL).toString()
    }

    override fun onStart() {
        super.onStart()
        linkTitle = getLinkTitle()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_WEBVIEW_LINK_START,"$linkTitle Page")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_WEBVIEW_LINK_END,"$linkTitle Page")
    }
}